package com.alinesno.cloud.initializr.starter.gateway.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class Utils {
	
	/**
	 * yaml 转换成json
	 * 
	 * @param yaml
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 */
	public static Object convertToYaml(String yaml) throws JsonMappingException, JsonProcessingException {
		ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
		Object obj = yamlReader.readValue(yaml, Object.class);
		return obj ;
	}

	/**
	 * yaml 转换成json
	 * 
	 * @param yaml
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 */
	public static String convertYamlToJson(String yaml) throws JsonMappingException, JsonProcessingException {
		ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
		Object obj = yamlReader.readValue(yaml, Object.class);

		ObjectMapper jsonWriter = new ObjectMapper();
		return jsonWriter.writeValueAsString(obj);
	}

}
